package com.hptpd.questionnairesurvey.vo;

import com.google.common.collect.Lists;
import com.hptpd.questionnairesurvey.domain.User;
import com.hptpd.questionnairesurvey.utils.AbstractMyBeanUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019-07-15 08:51
 * \*
 * \* Description:
 * \
 *
 * @author waiterlong
 */
@Data
public class UserVo {
    private String id;

    private String realName;

    private String phone;

    private String validateCode;

    private String weChatName;

    private String weChatSex;

    private String weChatOpenId;

    private String weChatImgUrl;

    private Date registerTime;

    private Date lastLoginTime;

    private DepartmentVo departmentVo;
    
    public static List<UserVo> convertUser(List<User> users) {
        List<UserVo> userVos = Lists.newLinkedList();
        for (User user : users) {
            userVos.add(convertUser(user));
        }
        return userVos;
    }

    public static UserVo convertUser(User user) {
        if (null == user) {
            return null;
        }
        UserVo userVo = new UserVo();
        AbstractMyBeanUtils.copyProperties(user, userVo);
        userVo.setDepartmentVo(DepartmentVo.convertDepartment(user.getDepartment()));

        //手机安全
        if (StringUtils.isNotBlank(user.getPhone()) && user.getPhone().length() > 4) {
            String phone = user.getPhone();
            String startStr = phone.substring(0, 3);
            String endStr = phone.substring(phone.length() - 4, phone.length());
            userVo.setPhone(startStr + "****" + endStr);
        }
        return userVo;
    }
}
